package com.alipay.freeblog.web.controller;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/cookie.htm")
public class CookieController {

	@SuppressWarnings("unchecked")
	@RequestMapping(method = RequestMethod.GET, params = "do=show")
	public String show(ModelMap modelMap, HttpServletRequest request) {

		Cookie[] cookies = request.getCookies();
		modelMap.put("cookies", cookies);
		modelMap.put("header", "Header");
		modelMap.put("footer","Footer");
		return "show";
	}

	@RequestMapping(method = RequestMethod.POST, params = "do=add")
	public String add(String name, String value, HttpServletResponse response) {
		Cookie cookie = new Cookie(name, value);
		cookie.setMaxAge(1000000);
		response.addCookie(cookie);
		return "redirect:cookie.htm?do=show";
	}

	@RequestMapping(method = RequestMethod.GET, params = "do=delete")
	public String delete(String name, HttpServletResponse response) {
		Cookie cookie = new Cookie(name, "");
		cookie.setMaxAge(0);
		response.addCookie(cookie);
		return "redirect:cookie.htm?do=show";
	}
}
